﻿using System.Collections.Generic;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Moq;
using ReadingList.Contracts.DataContracts.QueryResults;
using ReadingList.Contracts.ServiceContracts;
using ReadingList.DataAccess.EntityFramework;
using ReadlingList.Services;
using ReadingList.Contracts.DataContracts.QueryParameters;
using ReadingList.Domain.DomainObjects;
using ReadingList.Domain.ViewModels;
using System.Linq;

namespace ReadingList.Tests
{
    /// <summary>
    /// Book Service Tests
    /// </summary>
    [TestClass]
    public class BookSearchTests
    {
        public BookSearchTests() {
            Mock<IBookService> mockBookService = new Mock<IBookService>();

            // Setup fake BookQueryResult objects
            List<BookQueryResult> books = new List<BookQueryResult>
            {
                new BookQueryResult{ Title = "Fake ASP .NET Book 1" },
                new BookQueryResult{ Title = "Fake ASP .NET Book 2" }
            };

            // Setup the mock service to return the mock objects
            mockBookService.Setup(ms => ms.FindBookByTitle("ASP .NET")).Returns(books);

            MockBookService = mockBookService.Object;

        }

        public readonly IBookService MockBookService;
        public readonly IUserService MockRepository;

        #region Additional test attributes
        //
        // You can use the following additional attributes as you write your tests:
        //
        // Use ClassInitialize to run code before running the first test in the class
        // [ClassInitialize()]
        // public static void MyClassInitialize(TestContext testContext) { }
        //
        // Use ClassCleanup to run code after all tests in a class have run
        // [ClassCleanup()]
        // public static void MyClassCleanup() { }
        //
        // Use TestInitialize to run code before running each test 
        // [TestInitialize()]
        // public void MyTestInitialize() { }
        //
        // Use TestCleanup to run code after each test has run
        // [TestCleanup()]
        // public void MyTestCleanup() { }
        //
        #endregion

        [TestMethod]
        public void can_search_amazon_books() {
            // Arrange
            string titleToSearch = "ASP .NET";

            // Act
            var result = this.MockBookService.FindBookByTitle(titleToSearch);

            // Assert
            Assert.AreNotEqual(0, result.Count);
        }

        [TestMethod]
        public void can_create_valid_user_book_view_model() {
            // Arrange
            Mock<User> mockUser = new Mock<User>();
            Mock<Book> mockBook = new Mock<Book>();

            User user = mockUser.Object;
            Book book = mockBook.Object;

            user.Username = "ryan";
            book.Title = "Pro ASP .NET MVC Framework";
            string topic = "Web Development";

            // Act
            var viewModel = new UserBookViewModel(user, book);
            viewModel.Topic = topic;

            // Validate the view model
            var errors = viewModel.GetErrors();
            int errorCount = errors.Count();

            // Assert        
            Assert.AreEqual(0, errorCount);
        }

        [TestMethod]
        public void can_create_invalid_user_book_view_model() {
            // Arrange
            Mock<User> mockUser = new Mock<User>();
            Mock<Book> mockBook = new Mock<Book>();

            User user = mockUser.Object;
            Book book = mockBook.Object;

            user.Username = "ryan";
            book.Title = "Pro ASP .NET MVC Framework";
            string topic = "Web Development";

            // Act
            var viewModel = new UserBookViewModel(user, book);
            //viewModel.Topic = topic;

            // Validate the view model
            var errors = viewModel.GetErrors();
            int errorCount = errors.Count();

            // Assert        
            Assert.IsTrue(errorCount > 0);
        }
    }
}
